package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.common.vo.excel.ExcelModelVO;
import com.bifang.module.base.common.vo.excel.ExcelVO;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.helper.ExcelCommonHelper;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.bifang.module.system.common.enums.CardTypeEnum;
import com.bifang.module.system.core.integration.impl.ExcelIntegrationImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EduTalentEnum;
import com.jwsoft.manager.common.enums.SchoolNatureEnum;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.common.vo.eduTalent.*;
import com.jwsoft.manager.core.dao.model.EduSchool;
import com.jwsoft.manager.core.dao.model.EduTalent;
import com.jwsoft.manager.core.dao.service.EduSchoolService;
import com.jwsoft.manager.core.dao.service.EduTalentService;
import com.jwsoft.manager.core.integration.EduTalentIntegration;
import com.jwsoft.manager.core.util.EduEnrollClearCacheHelper;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 人才子女审核业务实现类
 *
 * @author hhj
 * @since 2023-04-26
 */
@ApiService(funcCode = "eduTalent", title = "人才子女审核")
public class EduTalentIntegrationImpl implements EduTalentIntegration {

    @Autowired
    public EduTalentService eduTalentService;

    @Autowired
    private EduSchoolService eduSchoolService;

    @Autowired
    private EduHelper eduHelper;
    @Autowired
    private CommonIntegration commonIntegration;

    @Autowired
    private EduEnrollClearCacheHelper eduEnrollClearCacheHelper;

    @Autowired
    private DictHelper dictHelper;

    @Autowired
    public ExcelIntegrationImpl excelIntegration;
    @Value("${spring.servlet.multipart.location}")
    private String tempPath;

    @Autowired
    private ExcelCommonHelper excelCommonHelper;

    @Override
    @OpApi(funcCode = "eduTalent0001", title = "人才子女审核分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduTalentVO> getList(EduTalentQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<EduTalent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(vo.getYear()!=null,
                        EduTalent::getYear,
                        vo.getYear())
                .eq(StringUtils.isNotBlank(vo.getFullName()),
                        EduTalent::getFullName,
                        vo.getFullName())
                .eq(StringUtils.isNotBlank(vo.getCardType()),
                        EduTalent::getCardType,
                        vo.getCardType())
                .eq(StringUtils.isNotBlank(vo.getIdcard()),
                        EduTalent::getIdcard,
                        vo.getIdcard())
                .eq(StringUtils.isNotBlank(vo.getTalentFullName()),
                        EduTalent::getTalentFullName,
                        vo.getTalentFullName())
                .eq(StringUtils.isNotBlank(vo.getTalentCardType()),
                        EduTalent::getTalentCardType,
                        vo.getTalentCardType())
                .eq(StringUtils.isNotBlank(vo.getTalentIdcard()),
                        EduTalent::getTalentIdcard,
                        vo.getTalentIdcard())
                .eq(StringUtils.isNotBlank(vo.getTalentType()),
                        EduTalent::getTalentType,
                        vo.getTalentType())
                .eq(StringUtils.isNotBlank(vo.getSchoolNatureIntention()),
                        EduTalent::getSchoolNatureIntention,
                        vo.getSchoolNatureIntention())
                .eq(StringUtils.isNotBlank(vo.getConfigTypeNameIntention()),
                        EduTalent::getConfigTypeNameIntention,
                        vo.getConfigTypeNameIntention())
                .eq(StringUtils.isNotBlank(vo.getAuthStatus()),
                        EduTalent::getAuthStatus,
                        vo.getAuthStatus());
        List<EduTalent> list = eduTalentService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduTalent> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduTalentVO> resultList = Convert.toList(EduTalentVO.class, pageInfo.getList());
        // 转义数据字典值
        dictHelper.valueToName(resultList, Arrays.asList(EduTalentEnum.values()));
        for (EduTalentVO eduTalentVO : resultList) {
            eduTalentVO.setAreaName(dictHelper.getAreaNameByCode(eduTalentVO.getAreaCode()));
        }
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduTalent0002", title = "人才子女审核根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduTalentVO getById(EduTalentKeyVO vo) {
        EduTalent entity = eduTalentService.getById(vo.getTalentId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduTalentVO result = Convert.convert(EduTalentVO.class, entity);
        return result;
    }

    /**
     * 保存前统一校验
     */
    @Override
    @OpApi(funcCode = "eduTalent0003", title = "人才子女审核保存前校验", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduTalentVO vo) {
        if (StringUtils.isBlank(vo.getCardType())) {
            vo.setCardType(CardTypeEnum.idcard.getType());
        }
        if (StringUtils.isBlank(vo.getIdcard())) {
            throw new AppException("学生证件号码不能为空");
        }
        vo.setIdcard(vo.getIdcard().trim().toUpperCase());
        if (CardTypeEnum.idcard.getType().equals(vo.getCardType()) && !IdcardUtil.isValidCard(vo.getIdcard())) {
            throw new AppException("学生身份证号码有误");
        }
        if (ObjectUtils.isEmpty(vo.getFullName())) {
            throw new AppException("学生姓名不能为空！");
        } else {
            vo.setFullName(
                    Validator.isChinese(vo.getFullName())
                            ? vo.getFullName().replaceAll(" ", "").replaceAll(" ", "").trim()
                            : vo.getFullName().trim());
        }
        if (!CardTypeEnum.idcard.getType().equals(vo.getCardType())) {
            if (StringUtils.isBlank(vo.getSex())) {
                throw new AppException("学生性别不能为空！");
            }
            if (StringUtils.isBlank(vo.getBirthYmd())) {
                throw new AppException("学生出生日期不能为空！");
            }
            String birthday = "";
            if (vo.getBirthYmd().length() >= 8) {
                birthday = vo.getBirthYmd().replaceAll("-", "").replaceAll("/", "");
            } else if (vo.getBirthYmd().length() == 6) {
                birthday = vo.getBirthYmd() + "01";
            } else {
                throw new AppException("学生出生日期格式不正确");
            }
            vo.setBirthYmd(birthday);
        } else {
            vo.setBirthYmd(IdcardUtil.getBirth(vo.getIdcard()));
            int gender = IdcardUtil.getGenderByIdCard(vo.getIdcard());
            if (gender == 1) {
                // 男
                vo.setSex("1");
            } else {
                // 女
                vo.setSex("2");
            }
        }

        if (!ObjectUtils.isEmpty(vo.getTelphone())) {
            vo.setTelphone(vo.getTelphone().trim());
            if (!PhoneUtil.isMobile(vo.getTelphone()) && !PhoneUtil.isTel(vo.getTelphone())) {
                throw new AppException("联系方式有误");
            }
        } else {
            throw new AppException("联系方式不能为空");
        }

        if (StringUtils.isBlank(vo.getTalentType())) {
            throw new AppException("人才类型不能为空");
        }
        if (StringUtils.isBlank(vo.getEducationType())) {
            throw new AppException("学历（学位）不能为空");
        }
        if (StringUtils.isBlank(vo.getWorkUnit())) {
            throw new AppException("工作单位不能为空");
        }
        if (StringUtils.isBlank(vo.getNation())) {
            throw new AppException("民族不能为空");
        }
        if (StringUtils.isBlank(vo.getAddress())) {
            throw new AppException("居住地址不能为空");
        }
        if (StringUtils.isBlank(vo.getHouseholdPlace())) {
            throw new AppException("户籍地不能为空");
        }
        if (StringUtils.isBlank(vo.getTechnicalTitle())) {
            throw new AppException("职称/高级技师资格不能为空");
        }
        if (StringUtils.isBlank(vo.getRelationshipType())) {
            throw new AppException("与孩子的关系不能为空");
        }
        if (StringUtils.isBlank(vo.getStudyIntention())) {
            throw new AppException("就读意向不能为空");
        }
        if (StringUtils.isBlank(vo.getJobResume())) {
            throw new AppException("工作简历不能为空");
        }
        if (StringUtils.isBlank(vo.getTalentCardType())) {
            vo.setTalentCardType(CardTypeEnum.idcard.getType());
        }
        if (StringUtils.isBlank(vo.getTalentIdcard())) {
            throw new AppException("人才证件号码不能为空");
        }
        vo.setTalentIdcard(vo.getTalentIdcard().trim().toUpperCase());
        if (CardTypeEnum.idcard.getType().equals(vo.getTalentCardType()) && !IdcardUtil.isValidCard(vo.getTalentIdcard())) {
            throw new AppException("人才身份证号码有误");
        }
        if (ObjectUtils.isEmpty(vo.getTalentFullName())) {
            throw new AppException("人才姓名不能为空！");
        } else {
            vo.setTalentFullName(
                    Validator.isChinese(vo.getTalentFullName())
                            ? vo.getTalentFullName().replaceAll(" ", "").replaceAll(" ", "").trim()
                            : vo.getTalentFullName().trim());
        }

        if (StringUtils.isBlank(vo.getFileIdcard())) {
            throw new AppException("身份证照片不能为空");
        }
        if (StringUtils.isBlank(vo.getFileHousehold())) {
            throw new AppException("户口本照片不能为空");
        }

        if ("2".equalsIgnoreCase(vo.getTalentType())) {
            //企业人才
            if (StringUtils.isBlank(vo.getFileRecommendation())) {
                throw new AppException("企业推荐书不能为空");
            }
        }
        if (vo.getYear() == null) {
            vo.setYear(eduHelper.thisYear());
        }
        //判断学生信息是否存在
        LambdaQueryWrapper<EduTalent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduTalent::getIdcard, vo.getIdcard()).eq(EduTalent::getYear, vo.getYear())
                .ne(EduTalent::getAuthStatus, AuthStatusEnum.NO.getType())
                .ne(!ObjectUtils.isEmpty(vo.getTalentId()), EduTalent::getTalentId, vo.getTalentId());
        if (eduTalentService.count(queryWrapper) > 0) {
            throw new AppException("该学生信息已提交申请，请到我的申请记录查看结果");
        }

    }

    @Override
    @OpApi(funcCode = "eduTalent0004", title = "人才子女审核新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduTalent0004",
            operationName = "人才子女审核新增",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduTalentVO add(EduTalentVO vo) {
        //保存前校验
        checkSave(vo);
        if (StringUtils.isBlank(vo.getAreaCode())) {
            vo.setAreaCode(SessionUtil.getAreaCode());
        }

        String schoolIdIntention = vo.getSchoolIdIntention();
        String schoolIdAllocation = vo.getSchoolIdAllocation();
        if (!ObjectUtils.isEmpty(schoolIdIntention)) {
            EduSchool intention = eduSchoolService.getById(schoolIdIntention);
            vo.setSchoolNameIntention(intention.getSchoolName());
        }
        if (!ObjectUtils.isEmpty(schoolIdAllocation)) {
            EduSchool allocation = eduSchoolService.getById(schoolIdAllocation);
            vo.setSchoolNameAllocation(allocation.getSchoolName());
        }
        if (ObjectUtils.isEmpty(vo.getAreaCode())) {
            vo.setAreaCode(eduHelper.thisAreaCode());
        }
        vo.setSbSyncFlag("0");
        vo.setAuthTime(new Date());
        vo.setAuthUserId(SessionUtil.getUserId());
        vo.setAuthStatus(AuthStatusEnum.YES.getType());
        //保存数据
        EduTalent entity = Convert.convert(EduTalent.class, vo);
        eduTalentService.save(entity);
        eduEnrollClearCacheHelper.clearCacheMyApply(entity.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(entity.getCreateName());
        //将主键设置回vo使操作日志可以取到
        vo.setTalentId(entity.getTalentId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduTalent0009", title = "家长端人才子女就读申请提交", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduTalent0009",
            operationName = "家长端人才子女就读申请提交",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduTalentVO addApp(EduTalentVO vo) {
        if (StringUtils.isBlank(vo.getSchoolIdIntention())){
            throw new AppException("请选择就读意向学校");
        }
        checkSave(vo);
        //判断提交次数
        CommonVO commonVO = new CommonVO();
        commonVO.setParamKey("lock_edit_student_pre");
        int max = 3;
        String maxValue = commonIntegration.getValueByKey(commonVO);
        if (StringUtils.isNotBlank(maxValue)) {
            try {
                max = Integer.valueOf(maxValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        QueryWrapper<EduTalent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduTalent::getYear, vo.getYear())
                .eq(EduTalent::getIdcard, vo.getIdcard())
                .eq(StringUtils.isNotBlank(vo.getAreaCode()), EduTalent::getAreaCode, vo.getAreaCode())
                .eq(EduTalent::getAuthStatus, AuthStatusEnum.NO.getType());
        if (eduTalentService.count(queryWrapper) >= max) {
            throw new AppException("因您频繁进行人才子女就读申请，系统拒绝您再次提起申请。申请解锁请联系教育局相关工作人员！");
        }

        if (StringUtils.isNotBlank(vo.getSchoolIdIntention())){
            EduSchool eduSchool = eduSchoolService.getById(vo.getSchoolIdIntention());
            if (eduSchool == null){
                throw new AppException("请选择正确的学校");
            }
            vo.setSchoolNameIntention(eduSchool.getSchoolName());
            vo.setSchoolNatureIntention(eduSchool.getSchoolNature());
            vo.setAreaCode(eduSchool.getAreaCode());
            if (SchoolNatureEnum.mzg.getType().equals(eduSchool.getSchoolNature())){
                if ("330783".equals(eduSchool.getAreaCode())){
                    if (StringUtils.isBlank(vo.getConfigTypeNameIntention())){
                        throw new AppException("请选择招生类别");
                    }
                }
            }
        }else{
            vo.setAreaCode(eduHelper.thisAreaCode());
        }
        vo.setSbSyncFlag("0");
        vo.setAuthStatus(AuthStatusEnum.NODO.getType());
        //保存数据
        EduTalent entity = Convert.convert(EduTalent.class, vo);
        eduTalentService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setTalentId(entity.getTalentId());
        eduEnrollClearCacheHelper.clearCacheMyApply(entity.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(entity.getCreateName());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduTalent0005", title = "人才子女审核修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduTalent0005",
            operationName = "人才子女审核修改",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduTalentVO edit(EduTalentVO vo) {
        if (ObjectUtils.isEmpty(vo.getTalentId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduTalent oldEntity = eduTalentService.getById(vo.getTalentId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduTalent entity = Convert.convert(EduTalent.class, vo);
        eduTalentService.updateById(entity);
        eduEnrollClearCacheHelper.clearCacheMyApply(entity.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(entity.getCreateName());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduTalent0006", title = "人才子女审核根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduTalent0006",
            operationName = "人才子女审核删除",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduTalentKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getTalentId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduTalent entity = eduTalentService.getById(vo.getTalentId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduTalentService.removeById(vo.getTalentId());
        eduEnrollClearCacheHelper.clearCacheMyApply(entity.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(entity.getCreateName());
    }

    @Override
    @OpApi(funcCode = "eduTalent0007", title = "人才子女信息审核", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduTalent0007",
            operationName = "人才子女信息审核",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void auditEduTalent(EduTalentAuditVO vo) {
        Long id = vo.getTalentId();
        EduTalent eduTalent = eduTalentService.getById(id);
        if (eduTalent == null) {
            throw new AppException("待审核的数据不存在");
        }
        if (!AuthStatusEnum.NODO.getType().equalsIgnoreCase(eduTalent.getAuthStatus())) {
            throw new AppException("该业务已审核，不能重复操作");
        }
        //更新审核状态
        eduTalent.setAuthStatus(vo.getAuthStatus());
        eduTalent.setAuthRemarks(vo.getAuthRemarks());
        eduTalent.setAuthTime(new Date());
        eduTalent.setAuthUserId(SessionUtil.getUserId());
        eduTalentService.updateById(eduTalent);
        eduEnrollClearCacheHelper.clearCacheMyApply(eduTalent.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(eduTalent.getCreateName());

    }

    @Override
    @OpApi(funcCode = "eduTalent0008", title = "人才子女信息退回到待审核", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduTalent0008",
            operationName = "人才子女信息退回到待审核",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void returnAuditEduTalent(EduTalentAuditVO vo) {
        Long id = vo.getTalentId();
        EduTalent eduTalent = eduTalentService.getById(id);
        if (eduTalent == null) {
            throw new AppException("退回到待审核的数据不存在");
        }
        if (AuthStatusEnum.NODO.getType().equalsIgnoreCase(eduTalent.getAuthStatus())) {
            throw new AppException("该业务已是未审核状态，不能重复操作");
        }
        //更新审核状态
        eduTalent.setAuthStatus(vo.getAuthStatus());
        eduTalent.setAuthRemarks(vo.getAuthRemarks());
        eduTalent.setAuthTime(null);
        eduTalent.setAuthUserId("");
        eduTalentService.updateById(eduTalent);
        eduEnrollClearCacheHelper.clearCacheMyApply(eduTalent.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(eduTalent.getCreateName());
    }

    @Override
    @OpApi(funcCode = "eduTalent0010", title = "管理端解锁人才子女申请锁定", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduTalent0010",
            operationName = "管理端解锁人才子女申请锁定",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    public void unLock(EduTalentKeyVO vo) {
        EduTalent eduTalent = eduTalentService.getById(vo.getTalentId());
        if (eduTalent == null) {
            throw new AppException("未找到需要解锁的数据");
        }
        if (AuthStatusEnum.BH.getType().equalsIgnoreCase(eduTalent.getAuthStatus())) {
            throw new AppException("该数据已做过解锁处理，请刷新页面");
        }
        if (!AuthStatusEnum.NO.getType().equalsIgnoreCase(eduTalent.getAuthStatus())) {
            throw new AppException("只有审核不通过数据才能解锁");
        }
        eduTalent.setAuthStatus(AuthStatusEnum.BH.getType());
        eduTalentService.updateById(eduTalent);
        eduEnrollClearCacheHelper.clearCacheMyApply(eduTalent.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(eduTalent.getCreateName());
    }

    @Override
    @OpApi(funcCode = "eduTalent0011", title = "人才子女分配学校", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduTalent0011",
            operationName = "人才子女分配学校",
            dataType = "talentId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void assignEduTalent(EduTalentAssignVO vo) {
        Long id = vo.getTalentId();
        EduTalent eduTalent = eduTalentService.getById(id);
        if (eduTalent == null) {
            throw new AppException("待分配学校的数据不存在");
        }
        //更新分配学校
        EduSchool eduSchool = eduSchoolService.getById(vo.getSchoolIdAllocation());
        eduTalent.setSchoolIdAllocation(vo.getSchoolIdAllocation());
        eduTalent.setSchoolNameAllocation(eduSchool.getSchoolName());
        eduTalentService.updateById(eduTalent);
        eduEnrollClearCacheHelper.clearCacheMyApply(eduTalent.getTalentIdcard());
        eduEnrollClearCacheHelper.clearCacheMyApplyByUserid(eduTalent.getCreateName());
    }

    @Override
    @OpApi(funcCode = "eduTalent0012", title = "人才子女信息导出", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void downloadData(EduTalentQueryVO vo, HttpServletResponse response) {
        ExcelVO excelVO = new ExcelVO();
        excelVO.setModelCode(vo.getModelCode());
        ExcelModelVO excelModelVO = excelIntegration.getModel(excelVO);

        String destFilePath = this.tempPath + "/" + IdUtil.fastSimpleUUID() + "." + excelModelVO.getExcelType();
        int pageNum = 1;
        setData(excelModelVO, null, destFilePath, vo, pageNum);
        String fileName = vo.getFileName();
        if (StringUtils.isBlank(fileName)) {
            fileName =
                    excelModelVO.getModelName()
                            + "_"
                            + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN)
                            + "."
                            + (StringUtils.isBlank(excelModelVO.getExcelType())
                            ? "xls"
                            : excelModelVO.getExcelType());
        }
        File file = FileUtil.file(destFilePath);
        BufferedInputStream in = null;
        try {
            in = FileUtil.getInputStream(file);
            ServletUtil.write(response, in, "application/octet-stream;charset=utf-8", fileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileUtil.del(destFilePath);
        }
    }

    private void setData(ExcelModelVO excelModelVO, ExcelWriter excelWriter, String destFilePath, EduTalentQueryVO vo, int pageNum) {
        vo.setPageNum(pageNum);
        vo.setPageSize(500);
        boolean closeFlag = false;
        try {
            PageInfo<EduTalentVO> pageInfo = getList(vo);

            String total = String.valueOf(pageInfo.getTotal());
            List<Map<String, Object>> list = new ArrayList<>();
            if (!CollectionUtils.isEmpty(pageInfo.getList())) {
                for (EduTalentVO eduTalentVO : pageInfo.getList()) {
                    list.add(BeanUtil.beanToMap(eduTalentVO, false, true));
                }
            }
            if (pageNum == 1) {
                if (excelWriter == null) {
                    excelWriter = excelCommonHelper.createExcelWriter(excelModelVO, destFilePath, Integer.parseInt(total));
                }
                excelCommonHelper.writeMap(excelWriter, list);
            } else {
                excelCommonHelper.writeMap(excelWriter, list);
            }
            if (pageInfo.isHasNextPage()) {
                pageNum = pageNum + 1;
                setData(excelModelVO, excelWriter, destFilePath, vo, pageNum);
            } else {
                closeFlag = true;
            }
        } catch (AppException appEx) {
            throw appEx;
        } catch (Exception e) {
            throw new AppException("调用自定义写入服务异常:" + e.getMessage());
        } finally {
            if (closeFlag && excelWriter != null) {
                excelWriter.close();
            }
        }
    }

}
